\chapter{User Programs}

An operating system usually consists of a kernel and some user programs (UPs). UPs only use the API to complete their goal. This section describes the UPs that are a part of PORN-OS.\\
There are several kinds of UPs. The biggest and most important one is the shell. Other programs can be started via the shell. It is the UP that is has to be started in order to make the OS usable. There are other UPs, such as a scroller that outputs text on the MALTA LCD and a program do demonstrate message passing.


\section{Shell}
\label{shell}

This program mainly provides means to start other processes, display information about them and output text to the Malta LCD display.

The shell is just a normal user program, that means it only uses functions from the API and can be started from another shell. Another implication is that it is possible to run multiple shells in parallel.

\subsection{Commands}The shell will read and parse the input given to it, using the lire() function, and match it to a table of commands and programs. If a command or program matching the input is found, it will execute the command or the program and either wait for the started program to return or return immediately, having the started program run in the background. The shell will also give information about the command just executed (eg. trying to executed a program that doesn't exist). This will use the ecrire() API function.\\Input can be given by writing a command or program name and finishing with <enter>. The backspace key can be used to correct mistakes.
        
The available programs will be given later, the available commands are as follows. Parameters will be displayed as <parameter1> <parameter2>.

\subsubsection{\texttt{help}}

Display all available commands with their options.

\subsubsection{\texttt{change\_priority <process\_id> <new\_priority>}}

Change the priority of the process with the given id to the new priority. It will use the changer\_priorite() API function.

\subsubsection{\texttt{delay\_process <process\_id> <time\_in\_secs>}}

Delay the process with the id <process\_id> for <time\_in\_secs> seconds. It will use the retarder() API function.

\subsubsection{\texttt{block\_process <process\_id>}}

Block the process with the given id. It will use the bloquer() API function.

\subsubsection{\texttt{kill <process\_id>}}

Terminates the process with the given id. It will use the tuer() API function.

\subsubsection{\texttt{echo <text>}}

Prints the given text on the Malta LCD display. It will use the ecrire\_LCD() API function.

\subsubsection{\texttt{time}}

Prints the current system time. It will use the obtenir\_temps() API function.

\section{Programs}
\label{programs}
\subsection{\texttt{ps [<process\_id>]}}

Shows information about all processes if <process\_id> is missing or detailed information about the process with the given id. It will use the obtenir\_tous\_pids() API function to get all pids of all the currently running processes and then use obtenir\_*() on each pid, printing it on the console using the ecrire() API function.

\subsection{\texttt{increment}}

\lstinputlisting[frame=single]{files/increment.c}

\subsection{\texttt{fibonacci}}

\lstinputlisting[frame=single]{files/fibonacci.c}

\subsection{\texttt{lcdscroll}}

\lstinputlisting[frame=single]{files/lcdscroll.c}

\subsection{\texttt{ring}}

The "ring" program consists of a parent program and child program. The parent sets up multiple copies of the child program and establishes a connection between pairs of them, in a way that a message will travel through all of them, but not twice between reaching the process it started from (called a ring). 
The children will then send one or more messages around, with a delay between receiving and sending. They will output if they have received or send a message, so you can follow the message(s) way through the ring.\\
API functions used for this program will be fourchette(), executer(), quitter(), retarder(), bloquer(), reveiller(), envoyer\_*() and recevoir\_message(). 

\subsection{\texttt{eat}}

The "eat" program is an example of the dining philosophers problem. There is a circle of philosophers. Between each pair of philosophers is one chopstick. Common knowledge is that you need two chopsticks to eat. So when one philosopher wants to eat, the ones besides him cannot, as they can't get two chopsticks.\\
The challenge of the dining philosophers problem is that it could come to dead locks: Each philosopher wants to eat at the same time and grabs the chopstick left to him. When he wants to grab the one right to him, that one is already gone. No philosopher can then eat and all of them starve to death. As philosophers are very important, we don't want that, so there has to be a solution to that.\\
The behaviour of a single philosopher is similar to the following (pseudocode taken from the requirements from the course website):
\begin{lstlisting}
think(random())
get one fork
get other fork
eat(random()) 
drop forks
restart
\end{lstlisting}
Our solution to the dining philosophers problem depends on writing messages without running into race conditions. We place the philosophers in a circle and numerate them, starting from 1. Each philosopher with an odd number starts taking the chopstick on his left, while each philosopher with an even number starts by taking a chopstick from his right.\\
In C pseudocode, the dining philosophers problem might be done like this:
\lstinputlisting[frame=single]{files/eat.c}
There will be one main program that sets up the shared resources and the communication between the philosphers. It will then return and go in the background, giving output about what the philosophers are doing.\\
API functions needed: fourchette() quitter(), retarder(), bloquer(), reveiller(), envoyer\_*() and recevoir\_message(). 
